package com.wangdong.wdbase.base;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.os.StrictMode;

import android.support.multidex.MultiDexApplication;


import com.wangdong.wdbase.exception.XTGlobalException;

import java.util.List;

/**
 * Created by luyz on 2017/7/17.
 * 基础application
 */

public class XTBaseApp extends MultiDexApplication {

    @SuppressLint("StaticFieldLeak")
    private static Context ctx;

    @Override
    public void onCreate() {
        super.onCreate();

        registerActivityLifecycleCallbacks(new XTLifeCycle());

        ctx = getApplicationContext();

        // android 7.0系统解决拍照的问题 否则需要在清单文件中共享
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
    }

    /**
     * Application初始化一次
     */
    public boolean shouldInit() {

        ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos;
        if (am != null) {
            processInfos = am.getRunningAppProcesses();

            String mainProcessName = getPackageName();
            int myPid = android.os.Process.myPid();
            for (ActivityManager.RunningAppProcessInfo info : processInfos) {
                if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                    return true;
                }
            }
        }

        return false;
    }

    public static Context getContext(){

        if (ctx == null) {
            throw new XTGlobalException(XTGlobalException.APPLICATION_CONTEXT_IS_NULL);
        }
        return ctx;
    }

    /* 如果是继承了 MultiDexApplication 可以重写此方法.否则重写就不用继承了
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // 将MultiDex注入到项目中
        MultiDex.install(this);
    }*/
}
